package imotech.ejb.dao;

import imotech.ejb.model.Cliente;
import imotech.ejb.model.Imovel;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class ClienteDAOBean
 */
@Stateless
public class ClienteDAOBean implements ClienteDAO {

	@PersistenceContext(unitName="imotech-ejb")
	private EntityManager em;
	
    /**
     * Default constructor. 
     */
    public ClienteDAOBean() {
        // TODO Auto-generated constructor stub
    }
    
	@Override
	public void cadastrar(Cliente cliente) {
		em.persist(cliente);
	}
	
//	@Override
	public List<Cliente> todos() {
		return em.createNamedQuery("findTodosClientes", Cliente.class).getResultList();
	}

	@Override
	public Cliente buscarId(int id) {
		// TODO Auto-generated method stub
		return em.find(Cliente.class, id);
	}

	@Override
	public void editar(Cliente cliente) {
		// TODO Auto-generated method stub
		em.merge(cliente);
	}

	@Override
	public List<Cliente> buscarPorNome(String nome) {
		return em.createNamedQuery("findClientePorNome", Cliente.class)
				.setParameter("nome", "%"+ nome.toLowerCase() +"%")
				.getResultList();
				
		/*return em.createQuery("SELECT c FROM Cliente c WHERE LOWER(c.nome) LIKE '%"+ nome.toLowerCase() +"%'", Cliente.class)
				.getResultList(); */
	}
	

}
